using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Globalization;


public class NumericTextBox : TextBox
{
    bool allowSpace = false;

    // Restricts the entry of characters to digits (including hex), the negative sign,
    // the decimal point, and editing keystrokes (backspace).
    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        base.OnKeyPress(e);

        NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
        string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
        string groupSeparator = numberFormatInfo.NumberGroupSeparator;
        string negativeSign = numberFormatInfo.NegativeSign;

        string keyInput = e.KeyChar.ToString();

        if (Char.IsDigit(e.KeyChar))
        {
            // Digits are OK
        }
        else if (e.KeyChar == '\b')
        {
            // Backspace key is OK
        }
        else if (this.allowSpace && e.KeyChar == ' ')
        {

        }
        else
        {
            // Consume this invalid key and beep
            e.Handled = true;
            //    MessageBeep();
        }
    }

    public int IntValue
    {
        get
        {
            return Int32.Parse(this.Text);
        }
    }

    public decimal DecimalValue
    {
        get
        {
            return Decimal.Parse(this.Text);
        }
    }

    public bool AllowSpace
    {
        set
        {
            this.allowSpace = value;
        }

        get
        {
            return this.allowSpace;
        }
    }
}
